package com.example.administrator.comdlfmytest;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ActivityInfo;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class RegisterActivity extends AppCompatActivity {

     private  TextView ti;
    private TextView title;
    private TextView back_btn;
    private Button btn_register;
    private EditText user_name_input, passwd_input, passwd_again_input;
    private String userName, psw, pswAgain;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        init();


    }

    private void init() {
        title = (TextView) findViewById(R.id.title);
        title.setText("注册");
        back_btn = (TextView) findViewById(R.id.back_btn);
        btn_register = (Button) findViewById(R.id.btn_register);
        user_name_input = (EditText) findViewById(R.id.user_name_input);
        passwd_input = (EditText) findViewById(R.id.passwd_input);
        passwd_again_input = (EditText) findViewById(R.id.passwd_again_input);


        back_btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                RegisterActivity.this.finish();
            }
        });


        btn_register.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getEditString();
                if (TextUtils.isEmpty(userName)) {
                    Toast.makeText(RegisterActivity.this, "请输入用户名", Toast.LENGTH_SHORT).show();
                    return;
                } else if (TextUtils.isEmpty(psw)) {
                    Toast.makeText(RegisterActivity.this, "请输入密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (TextUtils.isEmpty(pswAgain)) {
                    Toast.makeText(RegisterActivity.this, "请再次输入密码", Toast.LENGTH_SHORT).show();
                    return;
                } else if (!psw.equals(pswAgain)) {
                    Toast.makeText(RegisterActivity.this, "输入两次的密码不一样", Toast.LENGTH_SHORT).show();
                    return;
                } else if (isExistUserName(userName)) {
                    Toast.makeText(RegisterActivity.this, "此账户名已经存在", Toast.LENGTH_SHORT).show();
                    return;
                } else {
                    Toast.makeText(RegisterActivity.this, "注册成功", Toast.LENGTH_SHORT).show();

                    saveRegisterInfo(userName, psw);

                    Intent data = new Intent();
                    data.putExtra("userName", userName);
                    setResult(RESULT_OK, data);
                    RegisterActivity.this.finish();
                }
            }

            private void saveRegisterInfo(String userName, String psw) {
                SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
                SharedPreferences.Editor editor=sp.edit();//获取编辑器
                //以用户名为key，密码为value保存在SharedPreferences中
                editor.putString(userName,psw);
                editor.commit();//提交修改

            }
        });


    }

    /**
     * 获取控件中的字符串
     */
    private void getEditString() {
        userName = user_name_input.getText().toString().trim();
        psw = passwd_input.getText().toString().trim();
        pswAgain = passwd_again_input.getText().toString().trim();
    }




    private boolean isExistUserName(String userName){
        boolean has_userName=false;
        SharedPreferences sp=getSharedPreferences("loginInfo", MODE_PRIVATE);
        String spPsw=sp.getString(userName, "");
        if(!TextUtils.isEmpty(spPsw)) {
            has_userName=true;
        }
        return has_userName;
    }


}


